package leetcode.method.pointer;

/**
 * 乘法太多浪费计算时间
 */
public class Program121BuyAndSellTicket {

    public Program121BuyAndSellTicket() {
    }

    public int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int diff = 0;
        int front = 1;
        int after = 0;
        while (front < prices.length) {
            if (prices[front] <= prices[after]) {
                after = front;
            } else {
                diff = Math.max(prices[front] - prices[after], diff);
            }
            front++;
        }
        return diff;
    }

    public static void main(String[] args) {
        Program121BuyAndSellTicket myStack = new Program121BuyAndSellTicket();
        int[] prices = {7,1,5,3,6,4};
        System.out.println(myStack.maxProfit(prices));
    }
}
